/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package windowComponents;

import busEvents.StartSearchEvent;
import com.adamtaft.eb.EventBusService;
import com.adamtaft.eb.EventHandler;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextField;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author Arcanis
 */
public class SearchTexField extends JTextField {

    
    public SearchTexField() {
        setForeground(new Color(144, 144, 144));
        setText("Search...");
//        EventBusService.subscribe(this);
        
                

        addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {
                if(StringUtils.startsWith(getText(), "Search...")){
                    setText("");
                }   
            }

            @Override
            public void focusLost(FocusEvent e) {
                if(StringUtils.isBlank(getText())){
                    setText("Search...");
                }                   
            }
        });
        
        addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER){
                    EventBusService.publish(new StartSearchEvent(getText()));
                    setText("");
                }                
            }
        });
    }
}
